package com.ruoyi.parkinglot.controller;

import com.ruoyi.parkinglot.domain.ParkPaymentConfig;
import com.ruoyi.parkinglot.service.ParkPaymentConfigService;
import com.ruoyi.parkinglot.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/ParkPayymentConfig")
public class ParkPaymentConfigController {

    @Resource
    private ParkPaymentConfigService parkPaymentConfigService;

    /**
     * 查询支付配置列表
     */
    @GetMapping("/list")
    public Result list(ParkPaymentConfig config) {
        List<ParkPaymentConfig> list = parkPaymentConfigService.selectConfigList(config);
        return Result.success(list);
    }

    /**
     * 获取支付配置详细信息
     */
    @GetMapping(value = "/config/{configId}")
    public Result getInfo(@PathVariable("configId") Long configId) {
        return Result.success(parkPaymentConfigService.getById(configId));
    }

    /**
     * 新增支付配置
     */
    @PostMapping("/AddParkPayymentConfig")
    public Result add(@RequestBody ParkPaymentConfig config) {
        try {
            boolean success = parkPaymentConfigService.save(config);
            return success ? Result.success() : Result.error("新增失败");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 修改支付配置
     */
    @PutMapping("/UpParkPayymentConfig")
    public Result edit(@RequestBody ParkPaymentConfig config) {
        // 添加日志打印
        System.out.println("接收到的更新数据: " + config);
        try {
            // 尝试使用 MyBatis-Plus 的默认更新方法
            boolean success = parkPaymentConfigService.updateById(config);
            return success ? Result.success() : Result.error("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除支付配置
     */
    @DeleteMapping("/config/{configIds}")
    public Result remove(@PathVariable Long[] configIds) {
        return parkPaymentConfigService.deleteConfigByIds(configIds);
    }

    /**
     * 修改支付配置状态
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody ParkPaymentConfig config) {
        return parkPaymentConfigService.updateConfigStatus(config);
    }
}
